const express = require('express');
const router = express.Router();
const { Setting } = require('../../models');
const { NotFoundError } = require('../../utils/errors');
const { success, failure } = require('../../utils/responses');

/**
 * 公共方法：查询当前设置
 */
async function getSetting() {


   // 查询当前设置
  const setting = await Setting.findOne();

  // 如果没有找到，就抛出异常
  if (!setting) {
    throw new NotFoundError('初始系统设置未找到，请运行种子文件。')
  }

  return setting;
}
/**
 * 公共方法：白名单过滤
 * @param req
 * @returns {{title, content: (string|string|DocumentFragment|*)}}
 */
function filterBody(req) {
  return {
    name: req.body.name,
    icp: req.body.icp,
    copyright: req.body.copyright,
    

  };
}
/**
 * 查询系统详情
 */
router.get('/', async function (req, res) {
    try {
        const setting = await getSetting();
        success(res, '查询系统设置成功。', { setting });
        
    } catch (error) {
        failure(res, error);
    }
})
/**
 * 查询系统详情
 */
router.put('/', async function (req, res) {
    try {
        const setting = await getSetting();
        const body = filterBody(req);
        await setting.update(body)
        success(res, '更新系统设置成功。', { setting });
        
    } catch (error) {
        failure(res, error);
    }
})

module.exports = router;
